class BaseList {
    constructor(action, orderby, flag, $pagesize, $pagination, cokiePageSize, cokiePageIndex, cokieListResult, listUrl, delUrl) {
        this.first = 0;
        this.action = action;
        this.orderby = orderby;
        this.flag = flag;
        this.$pagesize = $pagesize;
        this.$pagination = $pagination;
        this.cokiePageSize = cokiePageSize;
        this.cokiePageIndex = cokiePageIndex;
        this.cokieListResult = cokieListResult;
        this.listUrl = listUrl;
        this.delUrl = delUrl;
        this.loading__indicator = $("#loading__indicator");
        this.loading__indicator_i = $("#loading__indicator i");
        this.showloadingwnd = false;
        this.pageData = null;
    }
    SetPageSize() {
        $.cookie(this.cokiePageSize, this.$pagesize.val());
        this.FetchForPage(1);
    }
    OnPageLoad() {
        let _pageindex = 1;
        if (this.cokiePageIndex !== null)
            _pageindex = $.cookie(this.cokiePageIndex) === undefined ? 1 : $.cookie(this.cokiePageIndex);
        if (this.flag === undefined || this.flag === null) {
            _pageindex = 1;
        } else {
            this.RestoreFilters();  // 恢复过滤条件
        }
        if (this.$pagination !== null) {
            this.$pagination.attr("_msh_page", _pageindex);
        }
        this.FetchForPage(_pageindex);
    }
    GetFilter(pageIndex) {
        if (pageIndex !== 0) {
            this.$pagination.attr("_msh_page", pageIndex);
        }
        let item = {};
        item['Limit'] = this.$pagesize === null ? 0 : parseInt(this.$pagesize.val());
        item['PageIndex'] = this.$pagination === null ? 0 : parseInt(this.$pagination.attr("_msh_page"));
        item['OrderBy'] = this.orderby;

        return item;
    }
    FetchForPage(pageIndex) {
        $.ajax({
            type: "POST",
            url: parent.basePath + this.listUrl,
            timeout: 30 * 1000,
            data: this.GetFilter(pageIndex),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: () => {
                // 显示刷新动画
                this.loading__indicator_i.addClass("fa-spin");
                this.loading__indicator.show();

                // 显示等待画面
                if (this.showloadingwnd)
                    showLoadmask();
            }
        }).done(data => {
            this.pageData = data;  // 设置本页数据
            if (data instanceof Array) {
                this.SetTableContent(data);
            } else {
                if (data.Success === true) {
                    this.BuildDataTable(data);
                } else {
                    this.EmptyTableContent();
                    ShowErrorMessagePanel("没有找到满足条件的数据。");
                    $('#loading').hide();
                }
            }
        }).fail(data => {
            $('#loading').hide();
            ajaxError(data, 1);
        }).always(() => {
            this.first = 1;
            // 隐藏刷新动画
            this.loading__indicator_i.removeClass("fa-spin");
            this.loading__indicator.hide();

            // 设置当前页
            if (this.$pagination !== null && this.cokiePageIndex !== null)
                $.cookie(this.cokiePageIndex, parseInt(this.$pagination.attr("_msh_page")));

            if (this.showloadingwnd)
                hideLoadmask();
        });
    }
    // 得到目录树的当前路径
    GetTreePath(nodeIdOfSelected) {
        let arrTreePath = new Array();
        arrTreePath.push(nodeIdOfSelected);
        let ref = $.jstree.reference('treeContent');
        let _nodeOfSelected = $("#" + nodeIdOfSelected);

        while (ref.get_parent(_nodeOfSelected) !== "#") {
            arrTreePath.push(ref.get_parent(_nodeOfSelected));
            nodeIdOfSelected = ref.get_parent(_nodeOfSelected);
            _nodeOfSelected = $("#" + nodeIdOfSelected);
        }
    }
    BuildDataTable(result) {
        if (result.TotalPages !== undefined) {
            // 设置翻页控件
            if (result.TotalPages > 0) {  //总页数
                $("#wrapper__navbar").data("swPagination").options.max_page = result.TotalPages;
            } else {
                $("#wrapper__navbar").data("swPagination").options.max_page = 1;
            }
            if (result.PageIndex > 0) { //当前页数
                $("#wrapper__navbar").data("swPagination").options.current_page = result.PageIndex;
                $("#wrapper__navbar").data("swPagination").setPage(result.PageIndex);
            } else {
                $("#wrapper__navbar").data("swPagination").options.current_page = 1; //当前页数
                $("#wrapper__navbar").data("swPagination").setPage(1);
            }
            // 得到数据列表
            result = result.DataList;
        }
        if (result === null) {
            $('#loading').hide();
            return false;
        }
        this.SetTableContent(result);

        $(window).resize();               // 防止因为出现滚动条而造成画面显示问题
        $('#loading').hide();
    }
    SetTableContent(result) {
        return result;
    }
    DoInsert() {
        // 设置当前页
        $.cookie(this.cokiePageIndex, parseInt(this.$pagination.attr("_msh_page")));
        // 保存过滤条件
        this.SaveFilters();
    }
    DoEdit() {
        // 设置当前页
        $.cookie(this.cokiePageIndex, parseInt(this.$pagination.attr("_msh_page")));
        // 保存过滤条件
        this.SaveFilters();
    }
    DoDelete(params, callback) {
        showModal(350, 140, '确认删除', '', null, null, 1, '请问，确定要删除当前记录吗？', () => {
            $.ajax({
                type: "POST",
                url: parent.basePath + this.delUrl,
                timeout: 30 * 1000,
                data: JSON.stringify(params),
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: () => {
                }
            }).done(data => {
                let _this = this;
                callback(_this, data);
            }).fail(data => {
                ajaxError(data, 1);
            }).always(( ) => {
            });
        });
    }
    SaveFilters() {

    }
    RestoreFilters() {

    }
    DoAjax(data, url, callback) {
        $.ajax({
            type: "POST",
            url: parent.basePath + url,
            timeout: 30 * 1000,
            data: JSON.stringify(data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            beforeSend: () => {
            }
        }).done(data => {
            callback(data, this);
        }).fail(data => {
            ajaxError(data, 1);
        }).always(( ) => {
        });
    }
    DoCheckAll($self, checkBoxName) {
        let _name = "input[type='checkbox'][name='" + checkBoxName + "']";
        let _this = this;
        if ($self.is(":checked") === true) {
            $(_name).each(function () {
                $(this).prop('checked', true);
                _this.SetRowStatus($(this));
            });
        } else {
            $(_name).each(function () {
                $(this).prop("checked", false);
                _this.SetRowStatus($(this));
            });
        }
    }
    DoCheckRow($self, checkBoxName, $checkall) {
        let _name = "input[type='checkbox'][name='" + checkBoxName + "']:checked";
        let _checkedRows = $(_name).length;
        if (_checkedRows < this.numCheckbox) {
            $checkall.prop("checked", false);
        } else {
            $checkall.prop("checked", true);
        }
        this.SetRowStatus($self);
    }
    AdjustRowStatus($self, disablerow, enablerow) {
        let $row = $self.parent().parent().parent();
        if ($self.is(":checked") === true) {
            $row.addClass("row__selected");
            disablerow($row, $row.index());
        } else {
            $row.removeClass("row__selected");
            enablerow($row, $row.index());
        }
    }
    SortTable(order_by) {
        this.orderby = order_by;
        this.FetchForPage(1);
    }
}